﻿using System.Windows;
using System.Windows.Input;
using MDCAdminTool.Controls;

namespace MDCAdminTool.Commands
{
    public class AddRegionCacheName : CommandBinding
    {
        public AddRegionCacheName()
        {
            Command = Application.Current.Resources["AddRegionCacheName"] as ICommand;
            CanExecute += OnCanExecute;
            Executed   += OnExecuted;
        }

        protected void OnExecuted( object sender , ExecutedRoutedEventArgs e )
        {
            var cache = e.Parameter as MdcCache;
            CacheRegion cr = null;

            if ( cache == null )
            {
                cr = e.Parameter as CacheRegion;

                if( cr != null )
                    cache = cr.Parent as MdcCache;
            }

            if ( cache != null )
            {
                var win = new AddRegionWin();
                win.ShowDialog();
                if ( win.IsSave )
                {
                    cache.AddRegion(win.RegionName, win.EvictionOn ?? false);
                }
                win.Close();
            }
        }
        protected void OnCanExecute( object sender , CanExecuteRoutedEventArgs e )
        {
            e.CanExecute = true;
        }
    }
}